<?php

namespace haibao\cms\data;

use haibao\cms\model\filter\icon as IconFilter;

class Icon extends \haibao\cms\data\BaseMysql{
    public function __construct(){
        parent::__construct('\haibao\cms\model\data\icon');
    }
    
    public function updateIconDataById($id,$status){
        $this->updateById($id, $status);
    }
    
    public function addIconInfo($addIconModel){
        $this->beginTranscation();
        try{
            $this->add($addIconModel);
        } catch(\Exception $e){
            throw $e;
        }
        $this->endtranscation();
        return $addIconModel;
    }
    
    public function updateIconModelData($editIconModel){
        $iconModel = $this->packIconFilter($editIconModel);
        $this->beginTranscation();
        try{
            $this->updateById($editIconModel->Id,$iconModel);
        } catch(\Exception $e){
            throw $e;
        }
        $this->endtranscation();
    }
    
    private function packIconFilter($editIconModel){
        $iconFilter =  new IconFilter();
        $iconFilter = array(
            IconFilter::CONDITION_FIELD_NAME_NAME_CN => $editIconModel->Name_Cn,
            IconFilter::CONDITION_FIELD_NAME_NAME_EN => $editIconModel->Name_En,
            IconFilter::CONDITION_FIELD_NAME_IMGID => $editIconModel->ImgId,
            IconFilter::CONDITION_FIELD_NAME_COUNTRY => $editIconModel->Country,
            IconFilter::CONDITION_FIELD_NAME_CITY => $editIconModel->City,
            IconFilter::CONDITION_FIELD_NAME_DESCRIPTION => $editIconModel->Description,
            IconFilter::CONDITION_FIELD_NAME_FOCUS => $editIconModel->Focus,
            IconFilter::CONDITION_FIELD_NAME_ARTICLE_ID => $editIconModel->ArticleId,
            IconFilter::CONDITION_FIELD_NAME_ARTICLE_URL => $editIconModel->ArticleUrl,
            IconFilter::CONDITION_FIELD_NAME_TYPE => $editIconModel->Type,
            IconFilter::CONDITION_FIELD_NAME_STATUS => $editIconModel->Status,
            IconFilter::CONDITION_FIELD_NAME_CREATETIME => $editIconModel->CreateTime,
            IconFilter::CONDITION_FIELD_NAME_PUBLISHTIME => $editIconModel->PublishTime,
        );
        return $iconFilter;
    }
    
    public function getPicPrimaryIdByPicId($articleId){
        $sql = "SELECT * FROM `ImageCollectiveTag` WHERE IsPrimaiy = 1 AND ImageCollectiveId = ".$articleId;
        $result = $this->query($sql);
        $primaryTag = array();
        while ($result->fetch_assoc()){
            $primaryTag[$result->getData('ImageCollectiveId')] = $result->getData('ParentId');
        }
        return $primaryTag;
    }
    public function getPicTitleByArticleId($articleId){
        $sql = "SELECT * FROM ImageCollective WHERE Id =".$articleId;
        $result = $this->query($sql);
        $picTitle = array();
        while ($result->fetch_assoc()){
            $picTitle[$result->getData('Id')] = $result->getData('Title');
        }
        return $picTitle;
    }
    
    public function exportData(){
        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE );
        $sql = "SELECT * FROM zt_icon";
        $result = $this->query($sql);
        if($result->getMysqlResult()->num_rows > 0){
            $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
            while($row = $result->fetch_assoc()){
                $sql = 'insert into `Icon` (Id,Name_Cn,Name_En,ImgId,Country,City,Description,Focus,ArticleId,ArticleUrl,Type,Status,PraiseNum,EditPraiseNum,CreateTime,PublishTime)
                 values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)';
                $valArr = array();
                if($result->getData('status')==1){
                    $status = 2;
                }elseif($result->getData('status')==0){
                    $status = 1;
                }
            
                $tempArr = preg_split("/\//", $result->getData('url'));
                $articleId = null;
                $Id = null;
                if(count($tempArr) == 5){
                    $temp = preg_split("/\./", $tempArr[4]);
                    $publishId = $temp[0];
                    if($result->getData('url')){
                        if($tempArr[3] == 'article'){
            
                            $type = 1;
                            $asql = "SELECT Id FROM Article WHERE PublishId = ".$publishId;
                            $resu = $this->query($asql);
                            while($row = $resu->fetch_assoc()){
                                $Id = $resu->getData('Id');
                            }
                            if($Id){
                                $articleId = $Id;
                            }
                        }elseif($tempArr[3] == 'pics'){
                            $type = 2;
                            $articleId = $publishId;
                        }
                    }
                }else{
                    $temp = preg_split("/\./", $tempArr[5]);
                    $publishId = $temp[0];
                    if($result->getData('url')){
                        if($tempArr[4] == 'article'){
                            $type = 1;
                            $asql = "SELECT Id FROM Article WHERE PublishId = ".$publishId;
                            $resu = $this->query($asql);
                            while($row = $resu->fetch_assoc()){
                                $Id = $resu->getData('Id');
                            }
                            if($Id){
                                $articleId = $Id;
                            }
                        }elseif($tempArr[4] == 'pics'){
                            $type = 2;
                            $articleId = $publishId;
                        }
                    }
                }
                array_push($valArr,
                $result->getData('id'),
                $result->getData('name_ch'),
                $result->getData('name_en'),
                '/'.$result->getData('image'),
                $result->getData('country'),
                $result->getData('city'),
                $result->getData('description'),
                $result->getData('focus'),
                $articleId,
                $result->getData('url'),
                $type,
                $status,
                $result->getData('zan'),
                $result->getData('zan'),
                $result->getData('datetime'),
                $result->getData('f_time')
                );
                $this->query($sql,$valArr);
            } 
        }
    }
}